package com.jt.www.biz.agree.service.impl;


import com.jt.www.biz.agree.service.AgreePrivacyLogService;
import com.jt.www.dao.mapper.agree.AgreePrivacyLogMapper;
import com.jt.www.domain.po.AgreePrivacyLogEntity;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.util.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

/**
 * @author lq
 * @create 2020-01-07 10:15
 */
@Service
public class AgreePrivacyLogServiceImpl implements AgreePrivacyLogService {

    @Autowired
    AgreePrivacyLogMapper agreePrivacyLogMapper;

    @Override
    public Integer saveAgreePrivacyLog(AgreePrivacyLogEntity agreePrivacyLogEntity) {

        if (agreePrivacyLogEntity == null) {
            return -1;
        }

        int num = 0;
        String currentTime = DateUtils.getCurrentTime();

        Weekend<AgreePrivacyLogEntity> weekend = Weekend.of(AgreePrivacyLogEntity.class);
        WeekendCriteria<AgreePrivacyLogEntity, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(AgreePrivacyLogEntity::getUserCode, agreePrivacyLogEntity.getUserCode());
        criteria.andEqualTo(AgreePrivacyLogEntity::getChannelCode, agreePrivacyLogEntity.getChannelCode());
        criteria.andEqualTo(AgreePrivacyLogEntity::getProtocolType, agreePrivacyLogEntity.getProtocolType());

        criteria.andEqualTo(AgreePrivacyLogEntity::getIsDeleted, false);
        int count = agreePrivacyLogMapper.selectCountByExample(weekend);

        // 已存在，则更新
        if (count > 0) {
            agreePrivacyLogEntity.setUpdateTime(currentTime);
            agreePrivacyLogEntity.setIsDeleted(false);
            num = agreePrivacyLogMapper.updateByPrimaryKeySelective(agreePrivacyLogEntity);
        } else {// 不存在，则新增
            agreePrivacyLogEntity.setCreateTime(currentTime);
            agreePrivacyLogEntity.setIsDeleted(false);
            num = agreePrivacyLogMapper.insertSelective(agreePrivacyLogEntity);
        }

        return num;
    }

    @Override
    public ResponseEntity searchAgreePrivacyLog(String userCode, String channelCode, String protocolType) {

        if (StringUtils.isBlank(userCode) || StringUtils.isBlank(channelCode)) {
            return null;
        }

        Weekend<AgreePrivacyLogEntity> weekend = Weekend.of(AgreePrivacyLogEntity.class);
        WeekendCriteria<AgreePrivacyLogEntity, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(AgreePrivacyLogEntity::getUserCode, userCode);
        criteria.andEqualTo(AgreePrivacyLogEntity::getChannelCode, channelCode);
        criteria.andEqualTo(AgreePrivacyLogEntity::getProtocolType, protocolType);

        criteria.andEqualTo(AgreePrivacyLogEntity::getIsDeleted, false);

        List<AgreePrivacyLogEntity> entities = agreePrivacyLogMapper.selectByExample(weekend);

        if (entities != null && entities.size() > 0) {
            return GenericDataResponse.okWithData(true);
        } else {
            return GenericDataResponse.okWithData(false);
        }
    }

    @Override
    public int searchAgreePrivacyLogCount(String userCode, String channelCode, String protocolType) {
        if (StringUtils.isBlank(userCode) || StringUtils.isBlank(channelCode)) {
            return 0;
        }

        Weekend<AgreePrivacyLogEntity> weekend = Weekend.of(AgreePrivacyLogEntity.class);
        WeekendCriteria<AgreePrivacyLogEntity, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(AgreePrivacyLogEntity::getUserCode, userCode);
        criteria.andEqualTo(AgreePrivacyLogEntity::getChannelCode, channelCode);
        criteria.andEqualTo(AgreePrivacyLogEntity::getProtocolType, protocolType);
        criteria.andEqualTo(AgreePrivacyLogEntity::getIsDeleted, false);

        return agreePrivacyLogMapper.selectCountByExample(weekend);
    }
}
